package com.kapcb.schedule.bean;

import com.kapcb.schedule.annotation.AutoXxlJob;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

/**
 * <a>Title: XxlJobInfo </a>
 * <a>Author: kapcb <a>
 * <a>Description: XxlJobInfo <a>
 *
 * @author Kpacb
 * @version 1.0
 * @date 2024/5/29 21:38
 * @since 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class XxlJobInfo implements Serializable {

    private static final long serialVersionUID = 674464258371230055L;

    /**
     * 任务主键 id
     */
    private int id;

    /**
     * 执行器主键 id
     */
    private int jobGroup;

    /**
     * 任务描述
     */
    private String jobDesc;

    /**
     * 新增时间
     */
    private Date addTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 负责人
     */
    private String author;

    /**
     * 报警邮件
     */
    private String alarmEmail;

    /**
     * 调度类型
     */
    private String scheduleType;

    /**
     * 调度配置，值含义取决于调度类型
     */
    private String scheduleConf;

    /**
     * 调度过期策略
     */
    private String misfireStrategy;

    /**
     * 执行器路由策略
     */
    private String executorRouteStrategy;

    /**
     * 执行器，任务 Handler 名称
     */
    private String executorHandler;

    /**
     * 执行器，任务参数
     */
    private String executorParam;

    /**
     * 阻塞处理策略
     */
    private String executorBlockStrategy;

    /**
     * 任务执行超时时间，单位秒
     */
    private int executorTimeout;

    /**
     * 失败重试次数
     */
    private int executorFailRetryCount;

    /**
     * GLUE 类型 #com.xxl.job.core.glue.GlueTypeEnum
     */
    private String glueType;

    /**
     * GLUE 源代码
     */
    private String glueSource;

    /**
     * GLUE 备注
     */
    private String glueRemark;

    /**
     * GLUE 更新时间
     */
    private Date glueUpdatetime;

    /**
     * 子任务ID，多个逗号分隔
     */
    private String childJobId;

    /**
     * 调度状态：0-停止，1-运行
     */
    private int triggerStatus;

    /**
     * 上次调度时间
     */
    private long triggerLastTime;

    /**
     * 下次调度时间
     */
    private long triggerNextTime;

    public XxlJobInfo(XxlJobGroup xxlJobGroup, XxlJob xxlJob, AutoXxlJob autoXxlJob) {
        XxlJobInfo xxlJobInfo = new XxlJobInfo();
        xxlJobInfo.setJobGroup(xxlJobGroup.getId());
        xxlJobInfo.setJobDesc(autoXxlJob.jobDesc());
        xxlJobInfo.setAuthor(autoXxlJob.author());
        xxlJobInfo.setScheduleType(autoXxlJob.scheduleType().name());
        xxlJobInfo.setScheduleConf(autoXxlJob.cron());
        xxlJobInfo.setGlueType("BEAN");
        xxlJobInfo.setExecutorHandler(autoXxlJob.value());
        xxlJobInfo.setExecutorRouteStrategy(autoXxlJob.executorRouteStrategy().name());
        xxlJobInfo.setMisfireStrategy("DO_NOTHING");
        xxlJobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION");
        xxlJobInfo.setExecutorTimeout(0);
        xxlJobInfo.setExecutorFailRetryCount(0);
        xxlJobInfo.setGlueRemark("GLUE代码初始化");
        xxlJobInfo.setTriggerStatus(autoXxlJob.triggerStatus());
    }
}
